<template>
	<view class="container">
		<view class="main">
			<view class="mobile">
				<text>{{tipsContent}}</text>
				<!-- <text>点击发送验证码到您的手机{{ userInfo.user.phonenumber.replace(userInfo.user.phonenumber.slice(3,7), '****')}}</text> -->
			</view>
			<view class="verifyCode" style="margin-bottom: 20rpx;">
				<input type="number" v-model="newPhone" placeholder="请输入新的手机号"/>
			</view>
			<view class="verifyCode">
				<input type="number" v-model="value" placeholder="请输入验证码"/>
				<!-- 发送验证码 -->
				<view v-if="!verificationFlag" class="no-send" @click="handleSendCode">发送验证码</view>
				<view v-if="verificationFlag" style="color: #a2a2a2;">已发送（{{ count }}S）</view>
			</view>
		</view>
		<view class="button" @click="determine">确定</view>
			
	</view>
</template>

<script>
	
	export default {
		data() {
			return {
				value: '',//输入的验证码
				timer: null,//验证码计时器
				count: 60,//验证码计时时间
				verificationFlag: false, // 是否发送过验证码
				tipsContent:'',
				newPhone:''
			}
		},
		methods: {
			// 获取提示文案
			async changePhoneTip(){
				let res = await this.$fetch(this.$api.changePhoneTip, {}, 'GET')
				if (res.code) return uni.showToast({ title: res.msg, icon: 'none' })
				this.tipsContent = res.data;
			},
			// 发送验证码 
			async handleSendCode() {
				if (this.newPhone == '') return uni.showToast({ title: '请输入手机号', icon: 'none' })
				this.verificationFlag = true
				const mobile = this.$jsencrypt.setEncrypt(this.$api.publicKey, this.newPhone);
				
				let res = await this.$fetch(this.$api.mobile_verification_code, {
					length: 6,
					mobile
				}, 'POST')
				if (res.code) return uni.showToast({
					title: res.msg,
					icon: 'none'
				})
				this.timer = setInterval(() => {
					if (this.count == 0) {
						clearInterval(this.timer)
						this.verificationFlag = false
						this.count = 60
					} else {
						this.count = --this.count
					}
				}, 1000)
			},
			// 确定
			async determine () {
				if (!this.newPhone || !this.value ) return uni.showToast({ title: '手机号或验证码不能为空', icon: 'none' })
				let res = await this.$fetch(this.$api.changePhone, { newPhone: this.newPhone , phoneCode:this.value}, 'POST','JSON')
				if (res.code ) return uni.showToast({ title: res.msg, icon: 'none' })
				uni.showToast({ title: '更换成功~', icon: 'none' })
				let timer = setTimeout(() => {
					clearTimeout(timer);
					timer = null;
					uni.navigateBack();
				}, 500);
			},
		},
		onLoad() {
			this.changePhoneTip();
		}
	}
</script>

<style lang="less">
.container {
	& > .main{
		padding: 30rpx;
		margin-top: 30rpx;
		margin-bottom: 140rpx;
		background-color: #FFFFFF;
		.mobile {
			display: flex;
			flex-direction: column;
			height: 40rpx;
			margin-bottom: 46rpx;
			& > text {
				color: rgba(64,33,16,0.50);
				margin-right: 30rpx;
			}
			// text:last-child {
			// 	font-weight: bold;
			// }
		}
		.verifyCode {
			border-radius: 30rpx;
			height: 100rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			background-color: #F2F2F2;
			padding: 0 30rpx;
			.no-send {
				color: #F39B19;
			}
			input {
				flex: 1;
			}
		}
	}
	& > .button {
		width: 690rpx;
		margin: 0 auto;
		height: 100rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		background-color: #FCC928;
		&:active {
			opacity: 0.7;
		}
	}
}
</style>
